/**
 * Copyright 2008 - 2009 Pro-Netics S.P.A.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package it.pronetics.madstore.hatom.alfresco.web.bean;

import it.pronetics.madstore.hatom.alfresco.util.SelectItemUtils;

import java.io.Serializable;

/**
 * Simple wrapper class to represent an hEntry data renderer
 * 
 * @author Salvatore Incandela
 */

public class HEntryBean implements Serializable {

	private static final long serialVersionUID = -13939843092308111L;
	private String title;
	private String summary;
	private String content;
	private String updated;
	private String author;
	private String name;

	public HEntryBean() {
		this.title = SelectItemUtils.NOT_SELECTED;
		this.summary = SelectItemUtils.NOT_SELECTED;
		this.content = SelectItemUtils.NOT_SELECTED;
		this.updated = SelectItemUtils.NOT_SELECTED;
		this.author = SelectItemUtils.NOT_SELECTED;
		resetName();
	}

	public HEntryBean(HEntryBean hEntry) {
		this.title = hEntry.getTitle();
		this.summary = hEntry.getSummary();
		this.content = hEntry.getContent();
		this.updated = hEntry.getUpdated();
		this.author = hEntry.getAuthor();
		resetName();
	}

	public HEntryBean(final String title, final String summary,
			final String content, final String updated, final String author) {
		this.title = title;
		this.summary = summary;
		this.content = content;
		this.updated = updated;
		this.author = author;
		resetName();
	}

	public void update(String entryTitle, String entrySummary,
			String entryContent, String entryUpdated, String entryAuthor) {
		this.title = entryTitle;
		this.summary = entrySummary;
		this.content = entryContent;
		this.updated = entryUpdated;
		this.author = entryAuthor;
		resetName();
	}

	private void resetName() {
		this.name = "hEntry_"
				+ (this.title + this.summary + this.content + this.updated + this.author)
						.hashCode();
	}

	@Override
	public String toString() {
		return "title: " + this.title + "\n" + "summary: " + this.summary
				+ "\n" + "content: " + this.content + "\n" + "updated: "
				+ this.updated + "\n" + "author: " + this.author + "\n";
	}

	@Override
	public boolean equals(Object obj) {
		if (obj instanceof HEntryBean) {
			HEntryBean entry = (HEntryBean) obj;
			return entry.getAuthor().equals(this.getAuthor())
					&& entry.getContent().equals(this.getContent())
					&& entry.getName().equals(this.getName())
					&& entry.getSummary().equals(this.getSummary())
					&& entry.getTitle().equals(this.getTitle())
					&& entry.getUpdated().equals(this.getUpdated());
		} else {
			return false;
		}
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
		resetName();
	}

	public String getSummary() {
		return summary;
	}

	public void setSummary(String summary) {
		this.summary = summary;
		resetName();
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
		resetName();
	}

	public String getUpdated() {
		return updated;
	}

	public void setUpdated(String updated) {
		this.updated = updated;
		resetName();
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
		resetName();
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getTitleDescription() {
		String[] propertyArray = this.title.split(" -- ");
		if (propertyArray.length == 1) {
			return this.title;
		}
		return propertyArray[1];
	}

	public String getSummaryDescription() {
		String[] propertyArray = this.summary.split(" -- ");
		if (propertyArray.length == 1) {
			return this.summary;
		}
		return propertyArray[1];
	}

	public String getContentDescription() {
		String[] propertyArray = this.content.split(" -- ");
		if (propertyArray.length == 1) {
			return this.content;
		}
		return propertyArray[1];
	}

	public String getUpdatedDescription() {
		String[] propertyArray = this.updated.split(" -- ");
		if (propertyArray.length == 1) {
			return this.updated;
		}
		return propertyArray[1];
	}

	public String getAuthorDescription() {
		String[] propertyArray = this.author.split(" -- ");
		if (propertyArray.length == 1) {
			return this.author;
		}
		return propertyArray[1];
	}

}
